//
//  ViewController.m
//  UnitScrollView
//
//  Created by wsl 王 on 2019/1/11.
//  Copyright © 2019年 wsl 王. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UIScrollViewDelegate>

{
    CGFloat _cellWidth;
    CGFloat _oldOffX;
    CGFloat _number;
    CGFloat _sep;
    NSArray * _titleArr;
}
@property (nonatomic,strong) UIScrollView *baseScrollView;
@property (nonatomic,strong) UIView *lineView;
@property (nonatomic,strong) NSMutableArray *viewArr;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.viewArr = [[NSMutableArray alloc] init];
    NSArray * titleArr = @[@"你",@"我",@"他"];
    _titleArr = titleArr;
    _number = titleArr.count;
    CGFloat width = self.view.bounds.size.width;
    CGFloat sep = 15;
    _sep = sep;
    CGFloat cellWidth = (width - (titleArr.count + 1) * 15) / titleArr.count;
    _cellWidth = cellWidth;
    UIView * segmentView = [[UIView alloc] initWithFrame:CGRectMake(0, 40, width, 50)];
    self.lineView = [[UIView alloc] initWithFrame:CGRectMake(0, segmentView.frame.size.height - 2, cellWidth, 2)];
    self.lineView.backgroundColor = [UIColor yellowColor];
    [segmentView addSubview:self.lineView];
    for (int i = 0; i < titleArr.count; i ++) {
        
        UILabel * lab = [[UILabel alloc] initWithFrame:CGRectMake(sep + (sep + cellWidth) * i , 0, cellWidth, segmentView.frame.size.height - 2)];
        lab.text = titleArr[i];
        lab.textAlignment = NSTextAlignmentCenter;
        if (i == 0) {
            
            lab.textColor = [UIColor redColor];
            self.lineView.center = CGPointMake(lab.center.x, self.lineView.center.y);
            _oldOffX = self.lineView.center.x;
        } else
        {
            lab.textColor = [UIColor lightGrayColor];
        }
        [self.viewArr addObject:lab];
        [segmentView addSubview:lab];
    }
    [self.view addSubview:segmentView];
    self.baseScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(segmentView.frame), width, 300)];
    self.baseScrollView.pagingEnabled = YES;
    self.baseScrollView.backgroundColor = [UIColor cyanColor];
    self.baseScrollView.delegate = self;
    self.baseScrollView.contentSize = CGSizeMake(width * _number, 0);
    [self.view addSubview:self.baseScrollView];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGPoint off = scrollView.contentOffset;
    int index = (off.x / scrollView.frame.size.width);
    CGFloat scra = ((off.x / scrollView.frame.size.width * _number)) / _number;
    CGFloat addX =  (_cellWidth +  _sep) * ((off.x / scrollView.frame.size.width * _number)) / _number;
    if (index + 1 < self.viewArr.count) {
        
        UILabel * lab1 = self.viewArr[index];
        lab1.font = [UIFont systemFontOfSize:17 - (scra - index) * 10];
        UILabel * lab2 = self.viewArr[index + 1];
        lab2.font = [UIFont systemFontOfSize:17 + (scra - index) * 10];
    }
    CGPoint center = self.lineView.center;
    self.lineView.center = CGPointMake(_oldOffX + addX, center.y);
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    CGPoint off = scrollView.contentOffset;
    int index = (off.x / scrollView.frame.size.width);
    UILabel * lab1 = self.viewArr[index];
    lab1.font = [UIFont systemFontOfSize:17 + 10];
    NSLog(@"%d",index);
    //在这里更改颜色
}


@end
